package com.example.zhangxun0601;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btn_load1;
    private ProgressBar pb_progre1;
    private Button btn_load2;
    private ProgressBar pb_progre2;
    private Button btn_load3;
    private ProgressBar pb_progre3;
    private Button btn_load4;
    private ProgressBar pb_progre4;
    private Button btn_load5;
    private ProgressBar pb_progre5;
    private Button btn_load6;
    private ProgressBar pb_progre6;
    private Button btn_load7;
    private ProgressBar pb_progre7;
    private Button btn_load8;
    private ProgressBar pb_progre8;
    private Button btn_load9;
    private ProgressBar pb_progre9;
    private Button btn_load10;
    private ProgressBar pb_progre10;
    private MyTask myTask;
    int pbNum;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        btn_load1 = (Button) findViewById(R.id.btn_load1);
        pb_progre1 = (ProgressBar) findViewById(R.id.pb_progre1);
        btn_load2 = (Button) findViewById(R.id.btn_load2);
        pb_progre2 = (ProgressBar) findViewById(R.id.pb_progre2);
        btn_load3 = (Button) findViewById(R.id.btn_load3);
        pb_progre3 = (ProgressBar) findViewById(R.id.pb_progre3);
        btn_load4 = (Button) findViewById(R.id.btn_load4);
        pb_progre4 = (ProgressBar) findViewById(R.id.pb_progre4);
        btn_load5 = (Button) findViewById(R.id.btn_load5);
        pb_progre5 = (ProgressBar) findViewById(R.id.pb_progre5);
        btn_load6 = (Button) findViewById(R.id.btn_load6);
        pb_progre6 = (ProgressBar) findViewById(R.id.pb_progre6);
        btn_load7 = (Button) findViewById(R.id.btn_load7);
        pb_progre7 = (ProgressBar) findViewById(R.id.pb_progre7);
        btn_load8 = (Button) findViewById(R.id.btn_load8);
        pb_progre8 = (ProgressBar) findViewById(R.id.pb_progre8);
        btn_load9 = (Button) findViewById(R.id.btn_load9);
        pb_progre9 = (ProgressBar) findViewById(R.id.pb_progre9);
        btn_load10 = (Button) findViewById(R.id.btn_load10);
        pb_progre10 = (ProgressBar) findViewById(R.id.pb_progre10);

        btn_load1.setOnClickListener(this);
        btn_load2.setOnClickListener(this);
        btn_load3.setOnClickListener(this);
        btn_load4.setOnClickListener(this);
        btn_load5.setOnClickListener(this);
        btn_load6.setOnClickListener(this);
        btn_load7.setOnClickListener(this);
        btn_load8.setOnClickListener(this);
        btn_load9.setOnClickListener(this);
        btn_load10.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_load1:
                 myTask=new MyTask(pb_progre1,btn_load1);
                myTask.execute(1);
                break;
            case R.id.btn_load2:
                myTask =new MyTask(pb_progre2,btn_load2);
                myTask.execute(2);
                break;
            case R.id.btn_load3:
                myTask =new MyTask(pb_progre3,btn_load3);
                myTask.execute(3);
                break;
            case R.id.btn_load4:
                myTask =new MyTask(pb_progre4,btn_load4);
                myTask.execute(4);
                break;
            case R.id.btn_load5:
                myTask =new MyTask(pb_progre5,btn_load5);
                myTask.execute(5);
                break;
            case R.id.btn_load6:
                myTask =new MyTask(pb_progre6,btn_load6);
                myTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,6);
                break;
            case R.id.btn_load7:
                myTask =new MyTask(pb_progre7,btn_load7);
                myTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,7);
                break;
            case R.id.btn_load8:
                myTask =new MyTask(pb_progre8,btn_load8);
                myTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,8);
                break;
            case R.id.btn_load9:
                myTask =new MyTask(pb_progre9,btn_load9);
                myTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,9);
                break;
            case R.id.btn_load10:
                myTask =new MyTask(pb_progre10,btn_load10);
                myTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,10);
                break;
        }
    }
    private class MyTask extends AsyncTask<Integer,Integer,Void>{
        private String TAG = "MyTask";
        private ProgressBar pb;
        private Button btn;
        private String str;

        public MyTask(ProgressBar pb, Button btn) {
            this.pb = pb;
            this.btn = btn;
        }

        @Override
        protected void onPreExecute() {
            Log.e(TAG, "onPreExecute: " );
            pb.setVisibility(View.VISIBLE);
            str = btn.getText().toString();
            btn.setEnabled(false);
            btn.setText("正在下载");
        }

        @Override
        protected Void doInBackground(Integer... integers) {
            Log.e(TAG, "doInBackground: " );
            pbNum = integers[0];
            for (int i = 0; i <= 100; i++) {
                publishProgress(pbNum, i);
                SystemClock.sleep(100);
            }
            return null;
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            Log.e(TAG, "onProgressUpdate: " );
            pbNum = values[0];
            int progress = values[1];
            switch (pbNum) {
                case 1:
                    pb_progre1.setProgress(progress);
                    break;
                case 2:
                    pb_progre2.setProgress(progress);
                    break;
                case 3:
                    pb_progre3.setProgress(progress);
                    break;
                case 4:
                    pb_progre4.setProgress(progress);
                    break;
                case 5:
                    pb_progre5.setProgress(progress);
                    break;
                case 6:
                    pb_progre6.setProgress(progress);
                    break;
                case 7:
                    pb_progre7.setProgress(progress);
                    break;
                case 8:
                    pb_progre8.setProgress(progress);
                    break;
                case 9:
                    pb_progre9.setProgress(progress);
                    break;
                case 10:
                    pb_progre10.setProgress(progress);
                    break;

            }
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            Log.e(TAG, "onPostExecute: " );
            pb.setVisibility(View.GONE);
            btn.setEnabled(true);
            btn.setText("下载完成");
        }
    }
}
